package com.myapp.ruyixing.ui;

import java.util.List;

import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import com.android.volley.RequestQueue;
import com.android.volley.toolbox.ImageLoader;
import com.android.volley.toolbox.NetworkImageView;
import com.android.volley.toolbox.Volley;
import com.myapp.ruyixing.R;
import com.myapp.ruyixing.http.RYHttpClient;
import com.myapp.ruyixing.http.RYHttpResponseHandler;
import com.myapp.ruyixing.model.ShopSignin;
import com.myapp.ruyixing.model.ShopSignin.ShopSigninItem;
import com.myapp.ruyixing.ui.widget.TitleBar;
import com.myapp.ruyixing.utils.BitmapLruCache;

public class ShopRegistrationListActivity extends BaseActivity implements
		OnItemClickListener {

	private ListView mListView;
	private TextView mEmptyView;

	private String mShopId;
	private String mShopName;

	private RYHttpResponseHandler mHandler = new RYHttpResponseHandler() {

		@Override
		public void onSuccess(Object o) {
			ShopSignin data = (ShopSignin) o;

			if (data != null && data.items != null) {
				MyListAdapter adapter = new MyListAdapter(data.items);
				mListView.setAdapter(adapter);
			}
			mListView.setEmptyView(mEmptyView);
			((TitleBar) findViewById(R.id.title_bar)).showLoading(false);
		}

		@Override
		public void onFailure(Throwable throwable) {
			if (!isFinishing()) {
				Toast.makeText(getApplicationContext(), "加载失败",
						Toast.LENGTH_SHORT).show();
			}
		}
	};

	@Override
	protected void onCreate(Bundle arg0) {
		super.onCreate(arg0);

		setContentView(R.layout.activity_shop_registration_list);

		initView();

		((TitleBar) findViewById(R.id.title_bar)).showLoading(true);

		mShopId = getIntent().getStringExtra(ShopDetailActivity.INTENT_SHOP_ID);
		mShopName = getIntent().getStringExtra(
				ShopDetailActivity.INTENT_SHOP_NAME);
		if (TextUtils.isEmpty(mShopId) == true) {
			Toast.makeText(getApplicationContext(), "商户为空", Toast.LENGTH_SHORT)
					.show();
		} else {
			RYHttpClient.getShopSigninList(this, mHandler, mShopId);
		}

		if (TextUtils.isEmpty(mShopName) == false) {
			((TitleBar) findViewById(R.id.title_bar)).setTitleText("签到留言墙\n"
					+ mShopName);
		}
	}

	@Override
	public void onItemClick(AdapterView<?> parent, View view, int position,
			long id) {

	}

	private void initView() {

		mListView = (ListView) findViewById(android.R.id.list);
		mEmptyView = (TextView) findViewById(android.R.id.empty);
		mListView.setOnItemClickListener(this);

		((TitleBar) findViewById(R.id.title_bar))
				.setOnIconClickedListener(new OnClickListener() {

					@Override
					public void onClick(View v) {
						onBackPressed();
					}
				});

		((TitleBar) findViewById(R.id.title_bar))
				.setOnRightIconClickedListener(new OnClickListener() {

					@Override
					public void onClick(View v) {
						startActivity(new Intent(
								ShopRegistrationListActivity.this,
								ShopAddRegistrationActivity.class).putExtra(
								ShopDetailActivity.INTENT_SHOP_ID, mShopId)
								.putExtra(ShopDetailActivity.INTENT_SHOP_NAME,
										mShopName));
						
						finish();
					}
				});
	}

	private class ViewHolder {
		TextView mUserTextView;
		TextView mRemarkView;
		TextView mTimeView;
		ImageView mScoreView;
		NetworkImageView mPicView;
	}

	private class MyListAdapter extends ArrayAdapter<ShopSigninItem> {

		private RequestQueue mQueue;
		private ImageLoader mImageLoader;

		public MyListAdapter(List<ShopSigninItem> list) {
			super(ShopRegistrationListActivity.this, -1, list);
			mQueue = Volley.newRequestQueue(ShopRegistrationListActivity.this);
			mImageLoader = new ImageLoader(mQueue, new BitmapLruCache());
		}

		@Override
		public View getView(int position, View convertView, ViewGroup parent) {
			if (convertView == null) {
				convertView = LayoutInflater.from(getContext()).inflate(
						R.layout.cell_shop_registration_list, parent, false);

				final ViewHolder holder = new ViewHolder();

				holder.mRemarkView = (TextView) convertView
						.findViewById(R.id.content);
				holder.mPicView = (NetworkImageView) convertView
						.findViewById(R.id.photo);
				holder.mUserTextView = (TextView) convertView
						.findViewById(R.id.username);
				holder.mScoreView = (ImageView) convertView
						.findViewById(R.id.score);
				holder.mTimeView = (TextView) convertView
						.findViewById(R.id.time);

				convertView.setTag(holder);
			}

			ViewHolder mHolder = (ViewHolder) convertView.getTag();
			ShopSigninItem item = getItem(position);

			mHolder.mUserTextView.setText(item.username);
			mHolder.mRemarkView.setText(item.content);
			mHolder.mTimeView.setText(item.datetime);
			mHolder.mScoreView.setImageLevel(item.score);
			if (!TextUtils.isEmpty(item.picurl)) {
				mHolder.mPicView.setVisibility(View.VISIBLE);
				mHolder.mPicView.setImageUrl(item.picurl, mImageLoader);
			} else {
				mHolder.mPicView.setVisibility(View.GONE);
			}

			return convertView;

		}
	}
}
